package com.dashu.core.service;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.utils.CopyUtil;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.VerificationUtil;
import com.dashu.core.dto.gate.GateDateDTO;
import com.dashu.core.handler.VerificationException;
import com.dashu.core.model.GateDate;
import com.dashu.core.model.QGateDate;
import com.dashu.core.vo.gate.GateDateVO;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GateDateService extends DataService {


    @Transactional
    public GateDateDTO add(GateDateVO gateDateVO) {
        GateDate gateDate = CopyUtil.copyEnty(gateDateVO, GateDate.class);
        try {
            long userId = Long.parseLong(VerificationUtil.getUserId());
            if (gateDateVO.getId() == 0)
                gateDate.setId(UIDUtil.nextId());
            if (gateDateVO.getCreateUser() == 0)
                gateDate.setCreateUser(userId);
            if (gateDateVO.getCreateTime() == 0)
                gateDate.setCreateTime(System.currentTimeMillis());
            entityManager.persist(gateDate);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        GateDateDTO gateDateDTO = CopyUtil.copyEnty(gateDate, GateDateDTO.class);
        return gateDateDTO;

    }

    @Transactional
    public boolean deleteGateData(long gateRuleId) {
        QGateDate qGateDate = QGateDate.gateDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qGateDate.ruleId.eq(gateRuleId));
        List<GateDate> gateDates =
                queryFactory
                        .select(qGateDate)
                        .from(qGateDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        gateDates.stream().forEach(ad -> {
            entityManager.remove(ad);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        });
        return true;

    }

    @Transactional
    public boolean updataGateData(List<GateDateVO> gateDateVOS, long gateRuleId) throws Exception {
        long userId = Long.parseLong(VerificationUtil.getUserId());
        deleteGateData(gateRuleId);
        gateDateVOS.stream().forEach(ad -> {
            ad.setUpdateUser(userId);
            ad.setUpdateTime(new Date().getTime());
            add(ad);
        });

        return true;

    }


    public List<GateDateDTO> findGateGroup(long gateRuleId) {
        QGateDate qGateDate = QGateDate.gateDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qGateDate.ruleId.eq(gateRuleId));
        List<GateDate> gateDates =
                queryFactory
                        .select(qGateDate)
                        .from(qGateDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        List<GateDateDTO> gateDateDTOS = CopyUtil.copyList(gateDates, GateDateDTO.class);
        return gateDateDTOS;

    }

    public List<GateDateDTO> findGateTime(long timeId) {
        QGateDate qGateDate = QGateDate.gateDate;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qGateDate.timeInId.eq(timeId));
        List<GateDate> gateDates =
                queryFactory
                        .select(qGateDate)
                        .from(qGateDate)
                        .where(booleanBuilder.getValue())
                        .fetch();
        List<GateDateDTO> gateDateDTOS = CopyUtil.copyList(gateDates, GateDateDTO.class);
        return gateDateDTOS;

    }

}
